<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;



use app\admin\model\ExpressModel;

class Express extends Base
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where[] = ['billcode', 'like', '%' . $param['searchText'] . '%'];
            }
            $user = new ExpressModel();
            $selectResult = $user->getUsersByWhere($where, $offset, $limit);

            // 拼装参数
            foreach($selectResult as $key=>$vo){

                // $selectResult[$key]['status']   = $vo->status;
                // $selectResult[$key]['express']  = $vo->express;
                // $selectResult[$key]['mailtype'] = $vo->mailtype;
                // $selectResult[$key]['username'] = $vo->memberModel()->value('memb_name');

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['billcode']));
            }
            $return['total'] = $user->getAllUsers($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($code)
    {
        $status = ExpressModel::where('billcode', $code)->value('status');
        if($status == 0){
            return [
                '出库' => [
                    'auth' => 'express/outstore',
                    'href' => "javascript:outstore(" .$code .")",
                    'btnStyle' => 'primary',
                    'icon' => 'fa fa-paste'
                ]
            ];
        }
    }

    /**
     * 批量入库
     */
    public function instore(){
        if(request()->isPost()){
            $file = request()->file('file');
            if(empty($file)) return msg(-1, '', '没有文件导入');
            $path = $file->getinfo()['tmp_name'];
            $spread = new \import\Expressdata;
            $res = $spread->import($path);
            if($res)
                return msg(0, '', '已完成入库：'. $res .'件');
            else
                return msg(-1, '', '没有需要入库的快件');
        }
        return view();
    }
    /**
    * 自动入库
    */
    public function autoInstore() {
        $path = env('app_path') . 'admin\view\yizhandata\express.xlsx';
        if(file_exists($path)){
            $spread = new \import\Expressdata;
            $res = $spread->import($path);
            unlink($path);
            if($res)
                return msg(0, '', '已完成入库：'. $res .'件');
            else
                return msg(-1, '', '没有需要入库的快件');
        }else{
            return msg(-1, '', '文件不存在');
        }
    }

    //单件
    public function oneInstore(){
        if(request()->isPost()){
            $company = [
                'ZTO'=>'中通快递', 'YTO'=>'圆通快递', 'SF'=>'顺丰快递', 'YUNDA'=>'韵达快递',
                'HTO'=>'汇通快递', 'STO'=>'申通快递', 'EMS'=>'EMS', 'ChinaPost'=>'中国邮政'
            ];
            $param = input('post.');
            $param['company'] = $company[$param['company_code']];
            $param['content'] = $param['content']. '您的包裹已到希望学院三食堂1楼“蜂鸟驿站”第三方代收点代收，请于19点前来领取。17380054687';
            $expre = new ExpressModel();
            return $expre->insertOne($param);
        }
    }

    /**
     * 出库
     */
     public function outstore(){
        if(request()->isAjax()){
            $code = input('param.');
            $exp = new ExpressModel;
            $res = $exp->outstore($code['billcode']);
            return json($res);
        }
        return view();
    }
}
